package com.xc.file.web.rest;


import com.xc.core.annotation.Authority;
import com.xc.core.bean.QueryBean;
import com.xc.core.model.TokenModel;
import com.xc.file.bean.FolderBean;
import com.xc.file.bean.QueryFolderBean;
import com.xc.file.bean.UpdateFolderBean;
import com.xc.file.config.Constants;
import com.xc.file.dto.FolderDto;
import com.xc.file.dto.FolderSizeDto;
import com.xc.file.entity.FolderEntity;
import com.xc.file.service.FolderService;
import com.xc.tool.utils.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>文件夹信息rest接口</p>
 *
 * @author xc
 * @version v1.0
 */
@Api(tags = "文件夹信息rest接口")
@RestController
public class FolderRest {

    @Autowired
    private FolderService folderService;
    @Autowired
    private Constants constants;

    @ApiOperation(value = "创建文件夹", notes = "创建当前用户的文件夹")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户token", name = "token", paramType = "header", required = true)
    })
    @PostMapping("/current_user/folder")
    @Authority
    public FolderDto createCurrentUserFolder(TokenModel tokenModel, @RequestParam(required = false) String folderId, @RequestBody FolderBean folderBean) {
        FolderEntity folderEntity = folderService.createFolder(tokenModel.getUserId(), folderId, tokenModel.getUserIds(), folderBean);
        return ObjectUtils.convert(new FolderDto(), folderEntity);
    }

    @ApiOperation(value = "修改文件夹", notes = "修改当前用户的文件夹")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户token", name = "token", paramType = "header", required = true)
    })
    @PutMapping("/current_user/folder/{folderId}")
    @Authority
    public FolderDto updateCurrentUserFolder(TokenModel tokenModel, @PathVariable String folderId, @RequestBody UpdateFolderBean updateFolderBean) {
        return folderService.updateFolder(tokenModel.getUserIds(), folderId, updateFolderBean);
    }

    @ApiOperation(value = "删除文件夹", notes = "删除当前用户的文件夹")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户token", name = "token", paramType = "header", required = true)
    })
    @DeleteMapping("/current_user/folder/{folderId}")
    @Authority
    public void deleteCurrentUserFolder(TokenModel tokenModel, @PathVariable String folderId) {
        folderService.deleteUserFolder(tokenModel.getUserIds(), folderId);
    }

    @ApiOperation(value = "查询文件夹集合", notes = "查询当前用户的文件夹集合")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户token", name = "token", paramType = "header", required = true)
    })
    @GetMapping("/current_user/folders")
    @Authority
    public List<FolderDto> getCurrentUserFolders(TokenModel tokenModel, @ModelAttribute QueryBean queryBean, @ModelAttribute QueryFolderBean queryFolderBean) {
        List<String> userIds = new ArrayList<>();
        FolderEntity folderEntity = new FolderEntity();
        if (!queryFolderBean.getOpenQueryAll()) {
            if (queryFolderBean.getFolderId() == null || "".equals(queryFolderBean.getFolderId())) {
                userIds = tokenModel.getUserIds();
                folderEntity.setParentNode(constants.getRoot());
            } else {
                FolderEntity folder = folderService.getTargetFolder(tokenModel.getUserId(), queryFolderBean.getFolderId());
                userIds.add(folder.getUserId());
                folderEntity.setParentNode(folder.getNode());
            }
        }
        folderEntity.setUserId(queryFolderBean.getUserId());
        folderEntity.setName(queryFolderBean.getName());
        List<FolderDto> folders = folderService.getUserFolderList(userIds, queryBean, folderEntity);
        // 路径处理
        if (!queryFolderBean.getOpenQueryAll()) {
            return folders;
        } else {
            for (FolderDto folderDto : folders) {
                folderDto.setPath(folderService.getPathByFolder(ObjectUtils.convert(new FolderEntity(), folderDto)));
            }
            return folders;
        }
    }

    @ApiOperation(value = "获取文件夹大小", notes = "获取当前用户文件夹大小")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户token", name = "token", paramType = "header", required = true),
            @ApiImplicitParam(value = "文件夹id", name = "folderId", paramType = "path", required = true),
    })
    @GetMapping("/current_user/folder/{folderId}/size")
    @Authority
    public FolderSizeDto getCurrentUserFolderSize(@PathVariable("folderId") String folderId) {
        return new FolderSizeDto(folderService.getFolderSize(folderId, null));
    }

    @ApiOperation(value = "获取父级文件夹集合", notes = "获取父级文件夹集合")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户token", name = "token", paramType = "header", required = true),
            @ApiImplicitParam(value = "文件夹id", name = "folderId", paramType = "query", required = true),
    })
    @GetMapping("/current_user/parent_folder_list")
    @Authority
    public List<FolderDto> getParentFolderList(@RequestParam String folderId) {
        List<FolderEntity> entities = folderService.getParentFolderList(folderId, null);
        return ObjectUtils.convertList(entities, FolderDto.class);
    }
}
